package com.huidu.bitrade.controller.businessRealization;


import com.fasterxml.jackson.annotation.JsonFormat;
import com.huidu.bitrade.constant.DepositStatusEnum;
import com.huidu.bitrade.constant.OtcBusinessConstants;
import com.huidu.bitrade.constant.PageModel;
import com.huidu.bitrade.controller.BaseController;
import com.huidu.bitrade.entity.QDepositRecord;
import com.huidu.bitrade.entity.QMember;
import com.huidu.bitrade.service.DepositRecordService;
import com.huidu.bitrade.util.MessageResult;
import com.huidu.bitrade.vo.DepositRecordVO;
import com.querydsl.core.types.Predicate;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("business/deposit-record")
@Slf4j
public class DepositRecordController extends BaseController {
	private Logger logger = LoggerFactory.getLogger(DepositRecordController.class);
    @Autowired
    private DepositRecordService depositRecordService;

    /**
     * 分页查询商家变现申请
     *
     * @param pageModel
     * @param account
     * @param status
     * @param startDate
     * @param endDate
     * @return
     */
    @GetMapping("page-query")
    public MessageResult pageQuery(
            PageModel pageModel,
            @RequestParam(value = "account", required = false) String account,
            @RequestParam(value = "status", required = false) DepositStatusEnum status,
            @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd") Date startDate,
            @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd") Date endDate) {
        List<Predicate> predicates = new ArrayList<>();
        predicates.add(QDepositRecord.depositRecord.member.id.eq(QMember.member.id));
        //查询type=1的记录
        predicates.add(QDepositRecord.depositRecord.type.eq(OtcBusinessConstants.DEPOSIT_RECORD_NEW));
        if (!StringUtils.isEmpty(account)) {
            predicates.add(QMember.member.username.like("%" + account + "%")
                    .or(QMember.member.mobilePhone.like("%" + account + "%"))
                    .or(QMember.member.email.like("%" + account + "%"))
                    .or(QMember.member.realName.like("%" + account + "%")));
        }
        if (status != null) {
            predicates.add(QDepositRecord.depositRecord.status.eq(status));
        }
        if (startDate != null) {
            predicates.add(QDepositRecord.depositRecord.createTime.goe(startDate));
        }
        if (endDate != null) {
            predicates.add(QDepositRecord.depositRecord.createTime.loe(endDate));
        }

        Page<DepositRecordVO> page = depositRecordService.joinFind(predicates, pageModel);
        return success(page);
    }

}
